package com.shengsiyuan.netty.sixthexample.client;

import com.shengsiyuan.netty.sixthexample.proto.MyDataInfo;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.Random;

public class TestClientHandler extends SimpleChannelInboundHandler<MyDataInfo.Message> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, MyDataInfo.Message msg) throws Exception {

    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {

        MyDataInfo.Message message = null;
        int randomInt = new Random().nextInt();
        if (0 == randomInt){
             message = MyDataInfo.Message.newBuilder()
                    .setType(MyDataInfo.Message.DataType.PERSON)
                    .setPerson(MyDataInfo.Person.newBuilder()
                            .setName("Person")
                            .setAge(18)
                            .setAddress("北京")
                            .build()
                    ).build();
        }else if (1 == randomInt){
            message = MyDataInfo.Message.newBuilder()
                    .setType(MyDataInfo.Message.DataType.TEACHER)
                    .setTeacher(MyDataInfo.Teacher.newBuilder()
                            .setName("Teacher")
                            .setAge(18)
                            .setAddress("北京")
                            .build()
                    ).build();
        }else {
            message = MyDataInfo.Message.newBuilder()
                    .setType(MyDataInfo.Message.DataType.STUDENT)
                    .setStudent(MyDataInfo.Student.newBuilder()
                            .setName("Student")
                            .setAge(18)
                            .setAddress("北京")
                            .build()
                    ).build();

        }



        MyDataInfo.Person person = MyDataInfo.Person.newBuilder()
                .setName("张三")
                .setAge(20).setAddress("北京").build();

        ctx.channel().writeAndFlush(message);
    }
}
